package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.object.pojo.Coupon;
import com.tbit.uqbike.service.baseAbstract.SynchronousService;
import com.tbit.uqbike.webmanager.dao.core.CouponDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 同步优惠券配置
 *
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2025-2-24
 */
@Service("couponsService")
@Slf4j
public class CouponsServiceImpl extends SynchronousService {

    @Resource
    private CouponDao couponDao;


    /**
     * 获取有效优惠券配置
     *
     * @param accountId 区域id
     * @return 优惠券配置
     */
    @Override
    protected List<?> queryConfig(Integer accountId) {
        List<Coupon> couponList = couponDao.getCouponByAccountId(accountId);
        if (CollUtil.isNotEmpty(couponList)) {
            return couponList;
        } else {
            return Collections.emptyList();
        }
    }

    /**
     * 删除优惠券配置
     *
     * @param accountId 区域id
     */
    @Override
    protected void deleteConfig(Integer accountId) {
        List<Coupon> couponList = couponDao.getCouponByAccountId(accountId);
        if (CollectionUtil.isNotEmpty(couponList)) {
            log.info("同步配置删除优惠券:{}", JSON.toJSONString(couponList));
            couponDao.deleteByAccountId(accountId);
        }
    }


    /**
     * 同步优惠券配置
     *
     * @param objects   配置列表
     * @param accountId 区域id
     */
    @Override
    protected void insertConfig(List<?> objects, Integer accountId) {
        if (CollectionUtil.isEmpty(objects)) {
            return;
        }
        List<Coupon> coupons = (List<Coupon>) objects;
        coupons.forEach(coupon -> coupon.setAccountId(accountId));
        couponDao.batchAdd(coupons);
    }
}
